// pages/order/orderReceipt/orderReceipt.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    work_order_id: '',
    is_repair: 0,//是否是维修
    imgUrl: [],//显示用的
    img_url: [],//传给服务器用的

    engineers: [],//返回的工程人员集合

    result: '',
    remark: '',
    user_id:'',
    contract_id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      work_order_id: options.work_order_id||'',
      contract_id: options.contract_id||'',
      user_id: options.user_id||'',
      is_repair: options.is_repair||0,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },


  //维修结果输入
  resultInput(e){
    var that = this;
    var val = e.detail.value;
    that.setData({
      result: val
    })
  },
  //备注输入
  remark(e) {
    var that = this;
    var val = e.detail.value;
    that.setData({
      remark: val
    })
  },
  // 添加图片 打开中间页
  openChoose(e) {
    var that = this;
    that.setData({
      isShow: true,
    })
  },
  //关闭中间页(关闭)
  closeShoot() {
    var that = this;
    that.setData({
      isShow: false,
    })
  },
  //打开相机
  openAbout(e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    //关闭中间页面 打开自定义照相机
    that.setData({
      isShow: false,
    })
    var length = that.data.imgUrl.length || 0;
    var count = 9 - length;
    var sourceType = '';
    if (type == 1) {
      sourceType = 'album';
    } else if (type == 2) {
      sourceType = 'camera';
    }
    wx.chooseImage({
      count: count,
      sizeType: ['compressed'],
      sourceType: [sourceType],
      success: function (res) {
        const tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          mask: true,
          title: '上传中',
        })
        that.uploadImageArr(tempFilePaths);
      },
    })
  },
  // 上传环境图 -多图
  uploadImageArr(tempFilePaths) {
    console.log('获取到的数据你好', tempFilePaths)
    var that = this;
    for (var i in tempFilePaths) {
      wx.uploadFile({
        url: app.globalData._url + 'Common/Common/upload',
        filePath: tempFilePaths[i],
        name: 'file',
        success: function (result) {
          if (result.statusCode !== 200) {
            wx.hideLoading();
            app.showToast('上传失败,请重新上传');
            return
          }
          var data = JSON.parse(result.data);
          if (data.code == 1000) {
            var imgUrl = that.data.imgUrl;
            var img_url = that.data.img_url;
            imgUrl.push(data.data.all_url);
            img_url.push(data.data.url);
            that.setData({
              imgUrl,
              img_url
            })
            wx.hideLoading();
          } else {
            wx.hideLoading();
            app.showToast('上传失败,请重新上传');
          }
        },
        fail: function (fail) {
          wx.hideLoading();
          app.showToast('上传失败,请重新上传');
        }
      })
    }
  },
  // 图片放大预览
  previewImage(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.url,
      urls: this.data.imgUrl // 需要预览的图片http链接列表
    })
  },
  // 删除图片
  reduceImg(e) {
    var imgUrl = this.data.imgUrl;
    var img_url = this.data.img_url;
    var index = e.currentTarget.dataset.index;
    imgUrl.splice(index, 1);
    img_url.splice(index, 1);
    this.setData({
      imgUrl,
      img_url
    })
  },

  //提交
  submit(){
    var that = this;
    app.ajax({
        url: 'Engineer/Contract/sign',
        method: "POST",
        data: {
          contract_id: that.data.contract_id,
          contact_type:3,
          sign_client:2,
          user_id:that.data.user_id,
          work_order_id: that.data.work_order_id,
          contract_imgs:that.data.img_url,
          remark:that.data.remark
        },
        success: function (res) {
          //console.log(res)
          if (res.data.code == 1000) {
            that.setData({
              isshow: true
            })
            app.showToast(res.data.message);
            wx.navigateBack({
              delta:1
            })
          } else {
            app.showToast(res.data.message);
          }
        }
      })
  },
})